package com.zhf.rabbitmq.topic;

import com.rabbitmq.client.*;
import com.zhf.rabbitmq.utils.RabbitConstant;
import com.zhf.rabbitmq.utils.RabbitUtil;

import java.io.IOException;

/**
 * @author: 曾鸿发
 * @create: 2022-01-08 22:04
 * @description：
 **/
public class Baidu {

    public static void main(String[] args) throws Exception{
        // 获取TCP长连接
        Connection connection = RabbitUtil.getConnection();
        final Channel channel = connection.createChannel();
        // 声明队列信息
        channel.queueDeclare(RabbitConstant.QUEUE_BAIDU, false, false, false, null);

        // 绑定队列和交换器，同时指定路由键
        channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "*.*.*.20201127");
        // channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.hunan.zhuzhou.20201127");
        // channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.heibei.shijiazhuang.20201128");
        // channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.hubei.wuhan.20201128");

        channel.basicQos(1);

        channel.basicConsume(RabbitConstant.QUEUE_BAIDU, false, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("新浪收到的天气信息：" + new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
